import base from '../api/base'
export const check = (prop) => {
  const result = []
  result.push(...arguments)
  result.map((item) => {
    if (item === false) {
      return
    }
  })
}
// 手机号检测
export const checkPhone = (phone) => {
  const reg = /^1[3456789]\d{9}/
  return reg.test(phone)
}

// loading
export const loading = (msg) => {
  wx.showLoading({
    title: msg,
    mask: true
  })
}
// tosat提示
export const toast = (msg, time) => {
  wx.showToast({
    title: msg,
    icon: 'none',
    mask: true,
    duration: time
  })
}

// hideLoading
export const hideLoading = (msg) => {
  wx.hideLoading()
}
// 空白检测
export const empty = (content) => {
  return content === ""
}

// 错误处理
export const catchError = () => {
  wx.showModal({
    title: '温馨提示',
    content: '您的操作过于频繁',
    showCancel: false
  })
}
// 重新登录
export const _reLogin = () => {
  wx.removeStorageSync('userInfo')
  wx.removeStorageSync('sessionKey')
  wx.removeStorageSync('location')
  wx.removeStorageSync('router')
  wx.removeStorageSync('changeLocation')
  wx.showModal({
    title: '温馨提示',
    content: '登录已经失效，请重新登录',
    showCancel: false,
    confirmText: '确认',
    success() {
      //非初始化进入该页面,且未授权
      wx.reLaunch({
        url: `/pages/login/index`,
      })
    }
  })
}

//获取头部信息
const _getHeader = () => {
  const sessionKey = wx.getStorageSync('sessionKey')
  if (sessionKey !== '') {
    return {
      'content-type': 'application/x-www-form-urlencoded',
      'Cookie': sessionKey,
      'app': 'weMini'
    }
  } else {
    return {
      'content-type': 'application/x-www-form-urlencoded',
      'app': 'weMini'
    }
  }
}

//普通用户封装请求函数 //默认为post 方法
export const request = (url, data, auth = false, methods = false) => {
  url = `${base.pro}${url}`
  const method = methods ? 'GET' : 'POST'
  const promise = new Promise((resolve, reject) => {
    wx.request({
      url,
      data,
      header: _getHeader(),
      method,
      success(res) {
        if (res.data.hasOwnProperty('success')) {
          if (res.data.msg === '请登录') _reLogin()
          res.data.success ? resolve(res) : reject(res)
        } else {
          res.statusCode === 200 ? resolve(res) : reject(res)
        }
      },
      fail(err) {
        wx.hideLoading()
        console.log("%c大唐云技术支持", " text-shadow: 0 1px 0 #ccc,0 2px 0 #c9c9c9,0 3px 0 #bbb,0 4px 0 #b9b9b9,0 5px 0 #aaa,0 6px 1px rgba(0,0,0,.1),0 0 5px rgba(0,0,0,.1),0 1px 3px rgba(0,0,0,.3),0 3px 5px rgba(0,0,0,.2),0 5px 10px rgba(0,0,0,.25),0 10px 10px rgba(0,0,0,.2),0 20px 20px rgba(0,0,0,.15);font-size:5em");
      }
    })
  })
  return promise
}
// 经纬度换算
const rad = (limit) => {
  return limit * Math.PI / 180.0 // 三角函数表示
}
// 距离换算
export const distance = (locationA, locationB) => {
  const radius = 6378.137 // 地球半径
  const latA = rad(locationA.lat)
  const latB = rad(locationB.lat)
  const lngA = rad(locationA.lng)
  const lngB = rad(locationB.lng)
  const latDifference = latA - latB // 纬度差
  const lngDifference = lngA - lngB // 经度差
  let distanceValue = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(latDifference / 2), 2) + Math.cos(latA) * Math.cos(latB) * Math.pow(Math.sin(lngDifference / 2), 2)))
  distanceValue = (distanceValue * radius).toFixed(2)
  return distanceValue
}
// 字符截断
export const ellipsis = (obj, size) => {
  if (obj.length > 0) {
    return obj.length > size ? obj.substring(0, size - 3) + '...' : obj
  }
}